Public Class MouseMove
    ' This code is based on a VB 6 version written by Saul Greenberg

    ' THINGS TO NOTE:
    ' ---------------
    ' be sure that there is a timer on the form

    ' Timer Code:
    '   mMove.ToNewPosition

    'MouseDown Code:
    '   If Button = 1 Then mMove.Begin Me.ActiveControl, X, Y

    'MouseUp Code:
    '   If Button = 1 Then mMove.EndIt

    'MouseMove Code:
    '   If Button = 1 Then mMove.InProgress X, Y

    'Form Load:
    '   mMove.Initialize movingTimer  ' where MovingTimer is the name of the timer on the form


    'the Current X and Y position
    Public curX As Single
    Public curY As Single

    'the last recorded X and Y position
    Private lastX As Single
    Private lastY As Single

    'the control being moved
    Public Control As Control

    'Whether the control is in the middle of being moved
    Private Moving As Boolean
    Private Sizing As Boolean
    Private Const Variance As Integer = 10
    Private timer As timer
    Private OriginalImage As Bitmap
    Private OrigW As Integer
    Private OrigH As Integer
    Private OrigX As Integer
    Private OrigY As Integer
    Private PrevX As Integer
    Private PrevY As Integer
    Private Enum ScaleType
        LeftRight
        UpDown
    End Enum
 
    Private ScaleDir As ScaleType


    ' Being the move
    Public Sub Begin(ByRef c As Control, ByVal X As Single, ByVal Y As Single)
        Control = c
        lastX = X
        lastY = Y

        If (X > c.Width - Variance) Or (X < Variance) Then
            Sizing = True
            OrigW = c.Width
            OrigH = c.Height
            OrigX = X
            OrigY = Y
            Dim P As PictureBox = c
            OriginalImage = P.Image
            ScaleDir = ScaleType.LeftRight
             
        ElseIf (Y > c.Height - Variance) Or (Y < Variance) Then
            Sizing = True
            OrigW = c.Width
            OrigH = c.Height
            OrigX = X
            OrigY = Y
            Dim P As PictureBox = c
            OriginalImage = P.Image
            ScaleDir = ScaleType.UpDown
             
        Else
            Moving = True

        End If

    End Sub
    Public Sub InProgress(ByVal X As Single, ByVal Y As Single)
        If Not Moving And Not Sizing Then Exit Sub

        timer.Enabled = True

        curX = X - lastX
        curY = Y - lastY
        PrevX = X
        PrevY = Y
    End Sub
    Public Sub EndIt()
        Control.BringToFront()
        Moving = False
        Sizing = False
        OriginalImage = Nothing
        timer.Enabled = False

    End Sub

    Private Function SizeImage(ByVal from_pic As Bitmap, ByVal NewW As Integer, ByVal NewH As Integer, Optional ByVal anti_alias As _
    Boolean = False) As Bitmap
        ' Make the destination Bitmap.
        If NewH > 0 And NewW > 0 Then
            Dim to_bm As New Bitmap(NewW, NewH)

            ' Copy the image.
            Dim gr As Graphics = Graphics.FromImage(to_bm)
            If anti_alias Then gr.InterpolationMode = _
                Drawing2D.InterpolationMode.HighQualityBilinear
            Try
                gr.DrawImage(from_pic, 0, 0, NewW - 1, NewH - 1)

            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try

            ' Display the result.
            Return to_bm
        Else
            Return from_pic
        End If

    End Function

    Public Sub ToNewPosition()
        Dim X As Single
        Dim Y As Single

        X = Control.Left + curX
        Y = Control.Top + curY

        If Moving Then
            Control.Location = New Point(X, Y)
        ElseIf Sizing Then
            Try
                Dim p As PictureBox = Control

                p.SizeMode = PictureBoxSizeMode.StretchImage
                Dim DistW As Integer = PrevX - OrigX
                Dim DistH As Integer = PrevY - OrigY
                Dim Factor As Double

                If (DistW > 0 Or DistH > 0) Then
                  
                    Select Case ScaleDir
                        Case ScaleType.LeftRight
                            Factor = (DistW + OrigW) / OrigW
                            Control.Width = OrigW * Factor
                            Control.Height = OrigH * Factor
                            p.Image = SizeImage(OriginalImage, Control.Width, Control.Height, True)
                        Case ScaleType.UpDown
                            Factor = (DistH + OrigH) / OrigH
                            Control.Width = OrigW * Factor
                            Control.Height = OrigH * Factor
                            p.Image = SizeImage(OriginalImage, Control.Width, Control.Height, True)
                    End Select
                Else
                    DistW = Math.Abs(DistW)
                    DistH = Math.Abs(DistH)
                    Select Case ScaleDir
                        Case ScaleType.LeftRight
                            Factor = (OrigW - DistW) / OrigW
                            Control.Width = OrigW * Factor
                            Control.Height = OrigH * Factor
                            p.Image = SizeImage(OriginalImage, Control.Width, Control.Height, True)
                        Case ScaleType.UpDown
                            Factor = (OrigH - DistH) / OrigH
                            Control.Width = OrigW * Factor
                            Control.Height = OrigH * Factor
                            p.Image = SizeImage(OriginalImage, Control.Width, Control.Height, True)
                    End Select

                End If
                Dim Image As Bitmap = p.Image
                Try
                    p.Region = BitmapToRegion(Image, Image.GetPixel(1, 1))
                Catch ex As Exception

                End Try

            Catch ex As Exception

            End Try



        End If




    End Sub

    Public Sub Initialize(ByVal tmr As timer)
        tmr.Enabled = False
        tmr.Interval = 30
        timer = tmr
    End Sub
End Class
